package py.tres_mosqueteros.eventos.whatson;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import py.tres_mosqueteros.eventos.entidades.Evento;
import py.tres_mosqueteros.eventos.entidades.Util;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PerfilActivity extends ActionBarActivity {
	Evento evento;
	String idEvento;
	ProgressBar spinnerPortrImage;

	// View viewPerfil;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile);
		overridePendingTransition(R.anim.slide_up_dialog, 0);
		final ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		// esto es para saber el eventoID
		// asi se le pasa las variables a los activities
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			idEvento = extras.getString("eventoId");

		}
		new infladorPerfilEvento().execute(this);
		// getWindow().getAttributes().windowAnimations =
		// R.style.Animations_arribaabajo;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private class infladorPerfilEvento extends
			AsyncTask<Context, Void, JSONObject> {

		JSONObject json3;
		Context mContext;

		@Override
		protected JSONObject doInBackground(Context... mContexto) {
			if (mContexto.length == 1) {
				mContext = mContexto[mContexto.length - 1];
				List<NameValuePair> parametros = new ArrayList<NameValuePair>();
				parametros.add(new BasicNameValuePair("idEvento", idEvento));
				spinnerPortrImage = (ProgressBar)findViewById(R.id.progressBar1);
				spinnerPortrImage.setVisibility(View.VISIBLE);

				json3 = Util.get("evento" + "/" + idEvento, parametros);
			}else{
				//no se le pudo enviar el contexto 
				return null;
			}

			return json3;

		}

		@Override
		protected void onPostExecute(JSONObject son) {

			Evento e;
			if (son != null) {
				e = new Evento(son);
				evento = e;
				this.inflarPerfil();
			}

		}

		protected void inflarPerfil() {

			spinnerPortrImage.setVisibility(View.GONE);
			ImageView imgView = (ImageView) findViewById(R.id.imageView1);
			imgView.setImageResource(R.drawable.whatsonlogo);
			imgView.setVisibility(View.VISIBLE);

			TextView tvNombrePerfil = (TextView) findViewById(R.id.textViewNombrePerfil);
			tvNombrePerfil.setText(evento.getNombre());

			TextView tvHashtag = (TextView) findViewById(R.id.textView2);
			tvHashtag.setText(evento.getNombre());

			TextView tvInformacion = (TextView) findViewById(R.id.textView1);
			tvInformacion.setText(evento.getDescripcion());

			// seteando las fotos
			// https://www.dropbox.com/s/jvoyas2c2s9mecx/01.jpg?dl=0
			GridView gridview = (GridView) findViewById(R.id.gridviewPerfil);
			gridview.setAdapter(new ImageAdapter3(mContext,3));

			gridview.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View v,
						int position, long id) {
					/*Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT)
							.show();*/
					Intent i = new Intent(getBaseContext(),
							FotosActivity.class);
					i.putExtra("eventoId", idEvento);
					startActivity(i);
				}
			});

			// falta setear comentarios

		}
	}

}
